package com.google.gwt.hmconsole.server;

import com.google.gwt.hmconsole.client.HostedModeService;
import com.google.gwt.hmconsole.client.to.BrowserConnectionInfoTO;
import com.google.gwt.hmconsole.client.to.HostedModeStateTO;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.util.ArrayList;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class HostedModeServiceImpl extends RemoteServiceServlet implements
    HostedModeService {

  private HostedModeState getState() {
    return (HostedModeState) this.getThreadLocalRequest().getAttribute(
        HostedModeConsoleServer.HOSTED_MODE_STATE);
  }

  public HostedModeStateTO getHostedModeState() {
    HostedModeState state = getState();
    ArrayList<BrowserConnectionInfoTO> connectionsTO = new ArrayList<BrowserConnectionInfoTO>();
    for (BrowserConnectionInfo conn : state.getBrowserConnections()) {
      connectionsTO.add(new BrowserConnectionInfoTO(conn.getBrowserName()));
    }

    return new HostedModeStateTO(connectionsTO);
  }

}
